// pages/receivingOrders/index.js
import api from '../../request/api.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav:['新订单','已接订单','已完成订单'],
    active:0,
    show: false,
    utils:[
      {
        icon:'../../images/utils_01.png',
        text:'请假',
        url:'../leave/index'
      },
      {
        icon:'../../images/utils_02.png',
        text:'积分钱包',
        url: '../myAccount/index'
      },
      {
        icon:'../../images/utils_03.png',
        text:'请假审核',
        url: '../leaveReview/index'
      },
      {
        icon:'../../images/utils_04.png',
        text: wx.getStorageSync('userInfo').isStaff ==1 ? '移交' : '入库',
        url: ''
      },
      {
        icon:'../../images/utils_05.png',
        text:'数据总览',
        url: '../OrderStatistics/index'
      },
    ],
    orders:[],
    pageNum:1,
    staff : 1,
  },
  checkedTab(e){
    const index = e.currentTarget.dataset.index
    this.clickGetOrders(index)
    this.setData({
      active:index,
      pageNum:1,
      orders : []
    })
  },
  //显示工具箱
  showUtils(){
    this.setData({
      show:true
    })
  },
  //关闭工具箱
  onClose(){
    this.setData({
      show: false
    })
  },
  //获取订单
  async getOrders (active){
    let { pageNum, orders} = this.data
    const { data: res } = await api('GET', 'Recycle/staff/getOrders', {
      token: wx.getStorageSync('token'),
      flag: active+1,
      pageNum
    })
    if (res.code == 200) {
      this.setData({
        orders: this.listArray(res.data)
      })
    }else{
      Toast(res.msg)
    }
  },
  // 列表数据处理
  listArray(res){
    var that = this;
    let list = that.data.orders;
    for (let i = 0; i < res.length; i++) {
      list.push({
        category_name : res[i].category_name,
        orderAddress : res[i].orderAddress,
        phone : that.data.staff == 1 ? res[i].orderAddress.addr_phone : res[i].staff_phone, 
        order_id : res[i].order_id,
        quantity : res[i].quantity,
        service_time : res[i].service_time ? res[i].service_time : '',
        state : res[i].state,
        state_name : that.stateExchange(res[i].state),
        unit : res[i].unit
      })
    };
    console.error(list);
    return list;  
  },
  //状态文字渲染
  stateExchange(state){
    let staff = (wx.getStorageSync('userInfo')).isStaff;
    if(staff == 1){
      switch (state) {
        case 1:
          return '进行中'
          break;
        case 2:
          return '已接单'
          break;
        case 3:
          return '已确认'
          break;
        case 4:
          return '已确认'
          break;
        case 9 : 
          return ''
          break;
        default:
          return '已完成'
          break;
      }
    }
    if(staff == 2){
      switch (state) {
        case 5:
          return '进行中'
          break;
        case 6:
          return '已接单'
          break;
        case 7:
          return '已确认'
          break;
        case 8:
          return '已入库'
          break;
        default:
          return ''
          break;
      }
    }
      
  },

  //接单
  async jieOrder(e){
    let order_id = e.currentTarget.dataset.id
    let {active} = this.data
    const { data: res } = await api('POST', 'Recycle/staff/acceptOrder', {
      token:wx.getStorageSync('token'),
      staff_id: wx.getStorageSync('userInfo').user_id,
      order_id
    })
    if (res.code === 200) {
      Toast(res.msg)
      this.clickGetOrders(active)
    }else{
      Toast(res.msg)
    }
  },
  async clickGetOrders(active) {
    let { orders } = this.data
    const { data: res } = await api('GET', 'Recycle/staff/getOrders', {
      token: wx.getStorageSync('token'),
      flag: active + 1,
      pageNum: 1
    })
    if (res.code === 200) {
      this.setData({
        orders: this.listArray(res.data)
      })
    } else {
      Toast(res.msg)
    }
  },
  savaOrderInfo(e){
    let order = e.currentTarget.dataset.order
    app.globalData.checkOrder = order
    wx.navigateTo({
      url: '../OrderRecovery/index',
    })
  },
  clickHandle(e) {
    const url = e.currentTarget.dataset.url
    if(url) {
      wx.navigateTo({
        url: url,
      })
    }else{
      this.postRuku()
    }
  },
  //移交入库
  async postRuku() {
    const { data: res } = await api('POST', '/Recycle/staff/transfer', {
      token: wx.getStorageSync('token')
    })
    if (res.code === 200) {
      Toast(res.msg)
    } else {
      Toast(res.msg)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrders(0)
    this.setData({
      staff :  wx.getStorageSync('userInfo').isStaff
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {pageNum, active} = this.data
    this.setData({
      pageNum: pageNum+1
    })
    this.getOrders(active)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})